package api

import (
	"blogwin.cn/framework/models"
	"github.com/gin-gonic/gin"
	"net/http"

)

type BookController struct {
	BaseController BaseController
}

func (con BookController) Index(c *gin.Context) {
	var filter models.Filter
	if err := c.BindJSON(&filter); err != nil {
		return
	}
	bookList := []models.Book{}
	db := models.Init()
	db = db.Where("size>0")
	if filter.Keyword != "" {
		db = db.Where("name like ?", "%"+ filter.Keyword +"%")
	}
	db.Offset(filter.Offset).Limit(filter.Count).Find(&bookList)
	for i, book := range bookList {
		book.SetSize()
		bookList[i] = book
	}
	c.JSON(http.StatusOK, gin.H{
		"data": bookList,
	})
}